#!/bin/bash

# --- 停止 manager-api ---
echo "Stopping manager-api..."
# 通过进程名查找
API_PID_BY_NAME=$(ps aux | grep 'spring-boot:run' | grep -v grep | awk '{print $2}')
if [ -n "$API_PID_BY_NAME" ]; then
  kill -9 $API_PID_BY_NAME
  echo "Killed manager-api (PID: $API_PID_BY_NAME) by process name."
fi

# 通过端口查找
API_PID_BY_PORT=$(lsof -t -i:8002)
if [ -n "$API_PID_BY_PORT" ]; then
  kill -9 $API_PID_BY_PORT
  echo "Killed manager-api (PID: $API_PID_BY_PORT) on port 8002."
fi

if [ -z "$API_PID_BY_NAME" ] && [ -z "$API_PID_BY_PORT" ]; then
    echo "manager-api is not running."
fi

echo "----------------------------------------"

# --- 停止 manager-web ---
echo "Stopping manager-web..."
# 通过进程名查找
WEB_PID_BY_NAME=$(ps aux | grep 'npm run serve' | grep -v grep | awk '{print $2}')
if [ -n "$WEB_PID_BY_NAME" ]; then
  kill -9 $WEB_PID_BY_NAME
  echo "Killed manager-web (PID: $WEB_PID_BY_NAME) by process name."
fi

# 通过端口查找
WEB_PID_BY_PORT=$(lsof -t -i:8001)
if [ -n "$WEB_PID_BY_PORT" ]; then
  kill -9 $WEB_PID_BY_PORT
  echo "Killed manager-web (PID: $WEB_PID_BY_PORT) on port 8001."
fi

if [ -z "$WEB_PID_BY_NAME" ] && [ -z "$WEB_PID_BY_PORT" ]; then
    echo "manager-web is not running."
fi

echo "----------------------------------------"
echo "All services stopped."